package com.whq.thrift.api.web.admin.db.requestlog;

import java.util.Map;

import org.apache.ibatis.jdbc.SqlBuilder;

import com.whq.common.StringUtil;

public class RequestLogSqlProvider {
	
	public String getSelectRecordsSql(Map<String, Object> parameter) {
        SqlBuilder.BEGIN();
        SqlBuilder.SELECT("*");
        SqlBuilder.FROM("t_request_log_"+parameter.get("tableSeq"));
        
        if (parameter.get("time") != null && StringUtil.isNotBlank(parameter.get("time").toString())) {
        	String startTime = (String)parameter.get("time") + " " + (String)parameter.get("start_time");
        	String endTime = (String)parameter.get("time") + " " + (String)parameter.get("end_time");
        	parameter.put("startTime", startTime);
        	parameter.put("endTime", endTime);
        	SqlBuilder.WHERE("time >= #{startTime} AND time <= #{endTime}");
        }
        
        if ((parameter.get("method")!= null) && (StringUtil.isNotBlank(parameter.get("method").toString())) ) {
        	SqlBuilder.WHERE("method = #{method}");
        }
        
        if ((parameter.get("phone") != null) && (StringUtil.isNotBlank(parameter.get("phone").toString()))) {
        	SqlBuilder.WHERE("phone = #{phone}");
        }
        
        if ((parameter.get("flow_id") != null)&& (StringUtil.isNotBlank(parameter.get("flow_id").toString()))) {
        	SqlBuilder.WHERE("flow_id = #{flow_id}");
        } 
        
        if ((parameter.get("app_version") != null)&& (StringUtil.isNotBlank(parameter.get("app_version").toString()))) {
        	SqlBuilder.WHERE("app_version = #{app_version}");
        }
        
        if ((parameter.get("os_type") != null)&& (StringUtil.isNotBlank(parameter.get("os_type").toString()))) {
        	SqlBuilder.WHERE("os_type = #{os_type}");
        }
        
        if ((parameter.get("verify_code") != null)&& (StringUtil.isNotBlank(parameter.get("verify_code").toString()))) {
        	SqlBuilder.WHERE("verify_code = #{verify_code}");
        }
        
        if ((parameter.get("result_code") != null)&& (StringUtil.isNotBlank(parameter.get("result_code").toString()))) {
        	SqlBuilder.WHERE("result_code = #{result_code}");
        }
        
        if (parameter.get("minCost") != null) {
        	SqlBuilder.WHERE("cost >= #{minCost}");
        }
        
        if (parameter.get("maxCost") != null) {
        	SqlBuilder.WHERE("cost <= #{maxCost}");
        }
        
        
        SqlBuilder.ORDER_BY("time desc");
        return SqlBuilder.SQL() + " limit #{beginIndex}, #{maxCount} ";
    }
	
	public String getSelectCountSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
        SqlBuilder.SELECT("count(*)");
        SqlBuilder.FROM("t_request_log_"+parameter.get("tableSeq"));
        
        if (parameter.get("time") != null) {
        	String startTime = (String)parameter.get("time") + " " + (String)parameter.get("start_time");
        	String endTime = (String)parameter.get("time") + " " + (String)parameter.get("end_time");
        	parameter.put("startTime", startTime);
        	parameter.put("endTime", endTime);
        	SqlBuilder.WHERE("time >= #{startTime} AND time <= #{endTime}");
        }
        
        if ((parameter.get("method")!= null) && (StringUtil.isNotBlank(parameter.get("method").toString())) ) {
        	SqlBuilder.WHERE("method = #{method}");
        }
        
        if ((parameter.get("phone") != null) && (StringUtil.isNotBlank(parameter.get("phone").toString()))) {
        	SqlBuilder.WHERE("phone = #{phone}");
        }
        
        if ((parameter.get("flow_id") != null)&& (StringUtil.isNotBlank(parameter.get("flow_id").toString()))) {
        	SqlBuilder.WHERE("flow_id = #{flow_id}");
        } 
        
        if ((parameter.get("app_version") != null)&& (StringUtil.isNotBlank(parameter.get("app_version").toString()))) {
        	SqlBuilder.WHERE("app_version = #{app_version}");
        }
        
        if ((parameter.get("os_type") != null)&& (StringUtil.isNotBlank(parameter.get("os_type").toString()))) {
        	SqlBuilder.WHERE("os_type = #{os_type}");
        }
        
        if ((parameter.get("verify_code") != null)&& (StringUtil.isNotBlank(parameter.get("verify_code").toString()))) {
        	SqlBuilder.WHERE("verify_code = #{verify_code}");
        }
        
        if ((parameter.get("result_code") != null)&& (StringUtil.isNotBlank(parameter.get("result_code").toString()))) {
        	SqlBuilder.WHERE("result_code = #{result_code}");
        }
        
        if (parameter.get("minCost") != null) {
        	SqlBuilder.WHERE("cost >= #{minCost}");
        }
        
        if (parameter.get("maxCost") != null) {
        	SqlBuilder.WHERE("cost <= #{maxCost}");
        }
        
        return SqlBuilder.SQL();
    }
	
	
	public String getStatByMethodCodeSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("method,result_code as verify_code, count(*)  as count ");
		SqlBuilder.FROM("t_request_log_"+parameter.get("tableSeq"));
		
        SqlBuilder.WHERE("time >= #{start_time} AND time <= #{end_time}");
		
		if (parameter.get("method") != null && StringUtil.isNotBlank(parameter.get("method").toString())) {
			SqlBuilder.WHERE("method = #{method}");
		}
		
		if (parameter.get("result_code") != null && StringUtil.isNotBlank(parameter.get("result_code").toString())) {
			SqlBuilder.WHERE("result_code = #{result_code}");
		}
		
		SqlBuilder.GROUP_BY("method, result_code");
		return SqlBuilder.SQL();
	}
	
	public String getStatByCodeSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("result_code as verify_code, count(*) as count ");
		SqlBuilder.FROM("t_request_log_"+parameter.get("tableSeq"));
		
		SqlBuilder.WHERE("time >= #{start_time} AND time <= #{end_time}");
		
		if (parameter.get("result_code") != null && StringUtil.isNotBlank(parameter.get("result_code").toString())) {
			SqlBuilder.WHERE("result_code = #{result_code}");
		}
		
		SqlBuilder.GROUP_BY("result_code");
		return SqlBuilder.SQL();
	}
	
	public String getIceCostTimeCountDataSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("method, result_code as verify_code, count(*) as count ");
		SqlBuilder.FROM("t_request_log_"+parameter.get("tableSeq"));
		
		SqlBuilder.WHERE("time >= #{endTime} AND time <= #{startTime}");
		
		SqlBuilder.GROUP_BY("method, result_code");
		return SqlBuilder.SQL();
	}

}
